package com.huawei.csi.tool.util;

import com.google.gson.reflect.TypeToken;
import com.huawei.csi.tool.config.LicenseConfig;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * JsonUtil
 *
 * @author lxw
 */
public class JsonUtil {
    /**
     * getLicenseConfig
     *
     * @param resourceName resourceName
     * @return java.util.Map<java.lang.String, com.huawei.csi.tool.config.LicenseConfig>
     **/
    public static Map<String, LicenseConfig> getLicenseConfig(String resourceName) {
        Type mapType = new TypeToken<List<LicenseConfig>>() {
        }.getType();
        Optional<Object> optionalObject = FileUtil.jsonFile2Class(resourceName, mapType);
        if (optionalObject.isPresent()) {
            Object object = optionalObject.get();
            List<LicenseConfig> configList = (List<LicenseConfig>) object;
            return configList.stream().collect(Collectors.toMap(LicenseConfig::getSpdxId, Function.identity()));
        }
        return new HashMap<>();
    }
}
